<!-- Select Organization -->
<!-- Author: Edwin -->
<template>
  <ejs-dropdowntree ref="dept-data-tree"
                    :fields='organizationFields'
                    :changeOnBlur="false"
                    :showCheckBox='false'
                    style="min-width:110px"
                    @change='onchange'
                    :placeholder="$t('custom.sys.search.dept')"
                    :allowMultiSelection='false'></ejs-dropdowntree>
</template>

<script>
export default {
  data () {
    return {
      organizationFields: {
        dataSource: [],
        value: 'id',
        text: 'name',
        child: 'children'
      }
    }
  },
  created () {
    this.getOrganizationTree()
  },
  methods: {
    getOrganizationTree () {
      this.$interface.system.getDeptListApi()
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          let tree = this.$refs['dept-data-tree'].ej2Instances.fields
          this.$set(tree, 'dataSource', res.data)
          this.$forceUpdate()
        }).catch(() => { })
    },
    onchange (e) {
      let defaultObj = this.$refs['dept-data-tree'].ej2Instances
      this.$emit('resetSearch', defaultObj.value)
    }
  }
}
</script>

<style scoped>

</style>
